package com.kancy.swing.lang;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * StringUtils
 *
 * @author kancy
 * @date 2020/2/16 5:56
 */
public class StringUtils {

    private static final String COMMA = ",";
    private static final String EMPTY_STRING = "";
    private static final String[] EMPTY_STRING_ARRAY = {};
    private static final String SPLIT_CHAR = COMMA;
    private static final String JOIN_CHAR = COMMA;

    /**
     * 空字符串
     * @return
     */
    public static String empty() {
        return EMPTY_STRING;
    }
    /**
     * 空字符串
     * @return
     */
    public static String comma() {
        return COMMA;
    }

    /**
     * 是否为空
     * @param object
     * @return
     */
    public static boolean isEmpty(String object){
        return Objects.isNull(object) || object.isEmpty();
    }

    /**
     * 是否不为空
     * @param object
     * @return
     */
    public static boolean isNotEmpty(String object){
        return !isEmpty(object);
    }

    /**
     * 是否空白
     * @param object
     * @return
     */
    public static boolean isBlank(String object){
        return Objects.isNull(object) || object.trim().isEmpty();
    }

    /**
     * 是否不是空白
     * @param object
     * @return
     */
    public static boolean isNotBlank(String object){
        return !isBlank(object);
    }

    /**
     * 添加字符串到字符数组
     * @param array
     * @param str
     * @return
     */
    public static String[] addStringToArray(String[] array, String str) {
        if (ObjectUtils.isEmpty(array)) {
            return new String[] {str};
        }
        String[] newArr = new String[array.length + 1];
        System.arraycopy(array, 0, newArr, 0, array.length);
        newArr[array.length] = str;
        return newArr;
    }

    /**
     * string 转 数组
     * @param str
     * @param splitChar
     * @return
     */
    public static String[] toArray(String str, String splitChar) {
        if (isNotEmpty(str) && isNotEmpty(splitChar)){
            return str.split(splitChar);
        }else {
            return EMPTY_STRING_ARRAY;
        }
    }

    public static String[] toArray(String str) {
        return toArray(str, StringUtils.SPLIT_CHAR);
    }

    /**
     * string 转 数组
     * @param str
     * @param splitChar
     * @return
     */
    public static List<String> toList(String str, String splitChar) {
        return Arrays.stream(toArray(str, splitChar))
                .collect(Collectors.toList());
    }

    public static List<String> toList(String str) {
        return toList(str, StringUtils.SPLIT_CHAR);
    }

    /**
     * string 转 数组
     * @param str
     * @param splitChar
     * @return
     */
    public static Set<String> toSet(String str, String splitChar) {
        return Arrays.stream(toArray(str, splitChar))
                .collect(Collectors.toSet());
    }

    public static Set<String> toSet(String str) {
        return toSet(str, StringUtils.SPLIT_CHAR);
    }

    /**
     * string 转 数组
     * @param stringArray
     * @param joinChar
     * @return
     */
    public static String join(String[] stringArray, String joinChar) {
        return Arrays.stream(stringArray).collect(Collectors.joining(joinChar));
    }

    public static String join(String[] stringArray) {
        return join(stringArray, StringUtils.JOIN_CHAR);
    }
}
